<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>点击按钮显示时间</title>
</head>

<body>
    <button>点击我显示时间</button>
    <div>现在时间</div>
</body>

<script>
    //当我们点击了按钮 div里卖弄的内容会发生变化
    //1）获取元素
    let btn = document.querySelector('button');
    let div = document.querySelector('div');
    //2)注册事件
    btn.onclick = () => {
            // div.innerText = '20220227';
            div.innerText = getTime();
        }
        //把之前写的获取日期封装成一个函数
    function getTime() {
        let date = new Date();
        //xx年xx月xx日 星期x
        let year = date.getFullYear();
        let yue = date.getMonth() + 1;
        let ri = date.getDate();
        let zhou = date.getDay();
        let arrZhou = ['星期日', '星期一', '星期二', '星期三', '星期四', '星期五', '星期六'];
        //console.log(`今天是  ${year}年 ${yue}月 ${ri}日 ${arrZhou[zhou]}`);
        //格式化时分秒 xx:xx:xx
        let shi = date.getHours();
        shi = shi < 10 ? '0' + shi : shi;
        let fen = date.getMinutes();
        fen = fen < 10 ? '0' + fen : fen;
        let miao = date.getSeconds();
        miao = miao < 10 ? '0' + miao : miao;
        return `今天是  ${year}年 ${yue}月 ${ri}日 ${arrZhou[zhou]} ${shi}:${fen}:${miao}`
    }
    //
</script>

</html>